package com.pig.goods.controller;

import com.pig.pojo.commgoods.Goods;
import com.pig.goods.service.GoodsService;
import com.pig.pojo.commgoods.JsonResult;
import com.pig.pojo.commgoods.PageObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/doFindAllGoods")
    public JsonResult doFindAllGoods(String title, Integer pageCurrent, Integer categoryId, Integer status) {
        PageObject<Goods> list = goodsService.findGoodsByTitle(title, status, categoryId, pageCurrent);
        System.out.println(list);
        return new JsonResult(list);
    }

    @GetMapping("/doFindGoodsPage")
    public JsonResult doFindGoodsPage(String title, Integer pageCurrent) {
        return new JsonResult(goodsService.findGoodsPages(title, pageCurrent));
    }

    @GetMapping("/doFindGoodById")
    public JsonResult doFindGoodsById(Integer goodsId) {

        return new JsonResult(goodsService.findGoodsById(goodsId));
    }

    @PostMapping("/doUpdateGoods")
    public JsonResult doUpdateGoods(Goods goods, Integer status) {
        goodsService.updateGoods(goods, status);
        return new JsonResult("修改成功！");
    }

    @PostMapping("/doSaveGoods")
    public JsonResult doSaveGoods(Goods goods) {
        goodsService.insertGoods(goods);
        return new JsonResult("添加成功！");
    }

    @GetMapping("/doSetGoodsStatus")
    public JsonResult doSetGoodsStatus(Integer goodsId, Integer status) {
        goodsService.setGoodsStatus(goodsId, status);
        return new JsonResult("设定成功！");
    }

    @GetMapping("/doDelGoods")
    public JsonResult doDelGoods(Integer goodsId) {
        goodsService.delGoods(goodsId);
        return new JsonResult("删除成功！");
    }
}
